﻿using System;
using System.Windows.Forms;

namespace InstallPartner
{
    /// <summary>
    /// This class provides the countdown dialog when waiting to automatically start the
    /// installation of a given profile.
    /// </summary>
    public partial class DelayStartDialog : Form
    {
        #region Private Attributes

        private readonly string _countDownText;
        private readonly string _profileName;
        private int _seconds = 30;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DelayStartDialog"/> class.
        /// </summary>
        /// <param name="profileName">The name of the profile that will be installed.</param>
        public DelayStartDialog(string profileName)
        {
            InitializeComponent();

            _profileName = profileName;

            // Define the RTF text to be displayed for the countdown message
            _countDownText = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\colortbl ;\red0\green0\blue128;\red0\green0\blue255;}
{\*\generator Msftedit 5.41.15.1515;}\viewkind4\uc1\pard\f0\fs20 The profile '\cf1\b PPP\cf0\b0 ' has been selected for installation.  The installation will start automatically in:\par
\par
\pard\qc\cf2\b\fs24 ### secondXXX\cf0\b0\fs20\par
\pard\par
Click \b Start Now\b0  to start the installation now or \b Cancel\b0  to exit.\par
}";

            UpdateDisplay();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Starts the installation.
        /// </summary>
        private void Start()
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        /// <summary>
        /// Updates the display.
        /// </summary>
        private void UpdateDisplay()
        {
            _seconds--;

            rtbCountdown.Rtf = _countDownText.Replace(
                "PPP", _profileName).Replace(
                "XXX", _seconds == 0 ? string.Empty : "s").Replace(
                "###", _seconds.ToString());

            if (_seconds <= 0)
            {
                Start();
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the Tick event of the secondsTimer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void secondsTimer_Tick(object sender, EventArgs e)
        {
            UpdateDisplay();
        }

        #endregion
    }
}
